package com.efast.dmom.mgmt.service.les.impl;

import com.efast.dmom.mgmt.bean.les.*;
import com.efast.dmom.mgmt.dao.les.HomePageDataDao;
import com.efast.dmom.mgmt.service.les.HomePageDataService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

/**
 * @author chlp
 */
@Service
public class HomePageDataServiceImpl implements HomePageDataService {

	private HomePageDataDao homePageDataDao;

	@Autowired
	public HomePageDataServiceImpl(HomePageDataDao homePageDataDao) {
		this.homePageDataDao = homePageDataDao;
	}

	/**
	 * 查询最近入库的数据
	 *
	 * @param day
	 *            day
	 */
	@Override
	public List<HomeTwDocAsnDetailsBean> queryRecentlyWarehousingData(Integer day) {
		return homePageDataDao.queryRecentlyWarehousingData(day);
	}

	/**
	 * 查询最近出库的数据
	 *
	 * @param day
	 *            最近多少天的
	 * @return List<HomeTwDocOrderDetailsBean>
	 */
	@Override
	public List<HomeTwDocOrderDetailsBean> queryRecentlyOutgoingData(Integer day) {
		return homePageDataDao.queryRecentlyOutgoingData(day);
	}

	/**
	 * 库存监控
	 *
	 * @return List<HomeTwInvSkuLoctlBean>
	 */
	@Override
	public List<HomeTwInvSkuLoctlBean> inventoryControl() {
		List<HomeTwInvSkuLoctlBean> homeTwInvSkuLoctlBeanList = homePageDataDao.inventoryControl();
		List<HomeTwInvSkuLoctlBean> resuleData = new ArrayList<>();
		for (HomeTwInvSkuLoctlBean homeTwInvSkuLoctlBean : homeTwInvSkuLoctlBeanList) {
			TwBasSkuMinMaxBean twBasSkuMinMaxBean = homePageDataDao.queryPartNoVersionMinMax(
					homeTwInvSkuLoctlBean.getPartNoVersion());
			homeTwInvSkuLoctlBean.setMinStorage(twBasSkuMinMaxBean.getMinStorage());
			homeTwInvSkuLoctlBean.setMaxStorage(twBasSkuMinMaxBean.getMaxStorage());
			resuleData.add(homeTwInvSkuLoctlBean);
		}
		return resuleData;
	}

	@Override
	public List<WarehousingTaskVolumeResultBean> warehousingTaskVolume() {

		List<WarehousingTaskVolumeResultBean> resultData = new ArrayList<>();

		// 仓储任务量趋势（按周、按任务类别统计，前5周）（收货、质检、上架、拣货、补货）
		// 【收货】
		WarehousingTaskVolumeBean recipient = homePageDataDao.queryRecipient();
		List<Integer> recipientData = new ArrayList<>();
		Integer two = recipient.getTwoWeekAgo() - recipient.getOneWeekAgo();
		Integer three = recipient.getThreeWeekAgo() - recipient.getTwoWeekAgo() - recipient.getOneWeekAgo();
		Integer four = recipient.getFourWeekAgo() - recipient.getThreeWeekAgo() - recipient.getTwoWeekAgo()
				- recipient.getOneWeekAgo();
		Integer five = recipient.getFiveWeekAgo() - recipient.getFourWeekAgo() - recipient.getThreeWeekAgo()
				- recipient.getTwoWeekAgo() - recipient.getOneWeekAgo();
		recipientData.add(recipient.getOneWeekAgo());
		if (two <= 0) {
			recipientData.add(0);
		} else {
			recipientData.add(recipient.getTwoWeekAgo() - recipient.getOneWeekAgo());
		}
		if (three <= 0) {
			recipientData.add(0);
		} else {
			recipientData.add(three);
		}
		if (four <= 0) {
			recipientData.add(0);
		} else {
			recipientData.add(four);
		}

		if (five <= 0) {
			recipientData.add(0);
		} else {
			recipientData.add(five);
		}
		WarehousingTaskVolumeResultBean recipientBean = new WarehousingTaskVolumeResultBean();
		recipientBean.setType("recipient");
		recipientBean.setData(recipientData);
		resultData.add(recipientBean);

		// 【质检】
		WarehousingTaskVolumeBean qualityTesting = homePageDataDao.queryQualityTesting();
		Integer qualityTestingTwo = qualityTesting.getTwoWeekAgo() - qualityTesting.getOneWeekAgo();
		Integer qualityTestingThree = qualityTesting.getThreeWeekAgo() - qualityTesting.getTwoWeekAgo()
				- qualityTesting.getOneWeekAgo();
		Integer qualityTestingFour = qualityTesting.getFourWeekAgo() - qualityTesting.getThreeWeekAgo()
				- qualityTesting.getTwoWeekAgo() - qualityTesting.getOneWeekAgo();
		Integer qualityTestingFive = qualityTesting.getFiveWeekAgo() - qualityTesting.getFourWeekAgo()
				- qualityTesting.getThreeWeekAgo() - qualityTesting.getTwoWeekAgo() - qualityTesting.getOneWeekAgo();
		List<Integer> qualityTestingData = new ArrayList<>();
		qualityTestingData.add(qualityTesting.getOneWeekAgo());
		if (qualityTestingTwo <= 0) {
			qualityTestingData.add(qualityTestingTwo);
		} else {
			qualityTestingData.add(qualityTestingTwo);
		}
		if (qualityTestingThree <= 0) {
			qualityTestingData.add(0);
		} else {
			qualityTestingData.add(qualityTestingThree);
		}
		if (qualityTestingFour <= 0) {
			qualityTestingData.add(0);
		} else {
			qualityTestingData.add(qualityTestingFour);
		}
		if (qualityTestingFive <= 0) {
			qualityTestingData.add(0);
		} else {
			qualityTestingData.add(qualityTestingFive);
		}
		WarehousingTaskVolumeResultBean qualityTestingBean = new WarehousingTaskVolumeResultBean();
		qualityTestingBean.setType("qualityTesting");
		qualityTestingBean.setData(qualityTestingData);
		resultData.add(qualityTestingBean);

		// 【上架】
		WarehousingTaskVolumeBean grounding = homePageDataDao.queryGrounding();
		Integer groundingTwo = grounding.getTwoWeekAgo() - grounding.getOneWeekAgo();
		Integer groundingThree = grounding.getThreeWeekAgo() - grounding.getTwoWeekAgo() - grounding.getOneWeekAgo();
		Integer groundingFour = grounding.getFourWeekAgo() - grounding.getThreeWeekAgo() - grounding.getTwoWeekAgo()
				- grounding.getOneWeekAgo();
		Integer groundingFive = grounding.getFiveWeekAgo() - grounding.getFourWeekAgo() - grounding.getThreeWeekAgo()
				- grounding.getTwoWeekAgo() - grounding.getOneWeekAgo();
		List<Integer> groundingData = new ArrayList<>();
		groundingData.add(grounding.getOneWeekAgo());
		if (groundingTwo <= 0) {
			groundingData.add(0);
		} else {
			groundingData.add(two);
		}
		if (groundingThree <= 0) {
			groundingData.add(0);
		} else {
			groundingData.add(three);
		}
		if (groundingFour <= 0) {
			groundingData.add(0);
		} else {
			groundingData.add(four);
		}
		if (groundingFive <= 0) {
			groundingData.add(0);
		} else {
			groundingData.add(five);
		}
		WarehousingTaskVolumeResultBean groundingDataBean = new WarehousingTaskVolumeResultBean();
		groundingDataBean.setType("grounding");
		groundingDataBean.setData(groundingData);
		resultData.add(groundingDataBean);

		// 【拣货】
		WarehousingTaskVolumeBean picking = homePageDataDao.queryPicking();
		Integer pickingTwo = picking.getTwoWeekAgo() - picking.getOneWeekAgo();
		Integer pickingThree = picking.getThreeWeekAgo() - picking.getTwoWeekAgo() - picking.getOneWeekAgo();
		Integer pickingFour = picking.getFourWeekAgo() - picking.getThreeWeekAgo() - picking.getTwoWeekAgo()
				- picking.getOneWeekAgo();
		Integer pickingFive = picking.getFiveWeekAgo() - picking.getFourWeekAgo() - picking.getThreeWeekAgo()
				- picking.getTwoWeekAgo() - picking.getOneWeekAgo();
		List<Integer> pickingData = new ArrayList<>();
		pickingData.add(picking.getOneWeekAgo());
		if (pickingTwo <= 0) {
			pickingData.add(0);
		} else {
			pickingData.add(pickingTwo);
		}
		if (pickingThree <= 0) {
			pickingData.add(0);
		} else {
			pickingData.add(pickingThree);
		}
		if (pickingFour <= 0) {
			pickingData.add(0);
		} else {
			pickingData.add(pickingFour);
		}
		if (pickingFive <= 0) {
			pickingData.add(0);
		} else {
			pickingData.add(pickingFive);
		}
		WarehousingTaskVolumeResultBean pickingDataBean = new WarehousingTaskVolumeResultBean();
		pickingDataBean.setType("picking");
		pickingDataBean.setData(pickingData);
		resultData.add(pickingDataBean);

		// 【补货】
		WarehousingTaskVolumeBean replenishment = homePageDataDao.queryReplenishment();
		Integer replenishmentTwo = replenishment.getTwoWeekAgo() - replenishment.getOneWeekAgo();
		Integer replenishmentThre = replenishment.getThreeWeekAgo() - replenishment.getTwoWeekAgo()
				- replenishment.getOneWeekAgo();
		Integer replenishmentFour = replenishment.getFourWeekAgo() - replenishment.getThreeWeekAgo()
				- replenishment.getTwoWeekAgo() - replenishment.getOneWeekAgo();
		Integer replenishmentFive = replenishment.getFiveWeekAgo() - replenishment.getFourWeekAgo()
				- replenishment.getThreeWeekAgo() - replenishment.getTwoWeekAgo() - replenishment.getOneWeekAgo();
		List<Integer> replenishmentData = new ArrayList<>();
		replenishmentData.add(replenishment.getOneWeekAgo());
		if (replenishmentTwo <= 0) {
			replenishmentData.add(0);
		} else {
			replenishmentData.add(replenishmentTwo);
		}
		if (replenishmentThre <= 0) {
			replenishmentData.add(0);
		} else {
			replenishmentData.add(replenishmentThre);
		}
		if (replenishmentFour <= 0) {
			replenishmentData.add(0);
		} else {
			replenishmentData.add(replenishmentFour);
		}
		if (replenishmentFive <= 0) {
			replenishmentData.add(0);
		} else {
			replenishmentData.add(replenishmentFive);
		}
		WarehousingTaskVolumeResultBean replenishmentBean = new WarehousingTaskVolumeResultBean();
		replenishmentBean.setType("replenishment");
		replenishmentBean.setData(replenishmentData);
		resultData.add(replenishmentBean);

		return resultData;
	}
}
